package com.jsonal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsonal.common.CommonUtil;
import com.jsonal.entity.api.ArticleTags;
import com.jsonal.entity.api.Tags;
import com.jsonal.entity.api.TagsBean;
import com.jsonal.mapper.TagsMapper;
import com.jsonal.service.TagsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsonal.vo.Result;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author auto
 * @since 2020-04-20
 */
@Service
public class TagsServiceImpl extends ServiceImpl<TagsMapper, Tags> implements TagsService {

    @Autowired
    private TagsMapper tagsMapper;

    @Override
    public Result getTagsList(TagsBean tagsBean) {
        Result result = new Result();
        List<Tags> tagsList;
        if (tagsBean == null) {
            tagsList = tagsMapper
                .selectList(new QueryWrapper<Tags>().eq("del_flag", false));
            result.setResults(tagsList);
        } else {
            IPage<Tags> tagsPage = new Page<>(tagsBean.getPage(), tagsBean.getPageSize());
            IPage<Tags> page = tagsMapper
                .selectPage(tagsPage, new QueryWrapper<Tags>().eq("del_flag", false));
            result.setObj(page);
            result.setResults(page.getRecords());
        }
        result.setMsg("查询成功");
        result.setSuccess(true);
        return result;

    }

    @Override
    public Result addOrUpdate(TagsBean tagsBean) {
        Result result = new Result();
        Tags tags = new Tags();
        if (tagsBean.getId() != null) {
            BeanUtils.copyProperties(tagsBean, tags);
            int update = tagsMapper.updateById(tags);
            if (update > 0) {
                result.setSuccess(true);
                result.setMsg("更新成功");
                return result;
            }
            result.setMsg("更新失败");
            result.setSuccess(false);
            return result;
        } else {
            BeanUtils.copyProperties(tagsBean, tags);
            tags.setDelFlag(0);
            tags.setCreateTime(new Date());
            tags.setGuid(CommonUtil.getGUID());
            int insert = tagsMapper.insert(tags);
            if (insert > 0) {
                result.setSuccess(true);
                result.setMsg("插入成功");
                return result;
            }
            result.setSuccess(false);
            result.setMsg("新增失败");
            return result;
        }
    }

    @Override
    public Result getTagById(Integer id) {
        Result result = new Result();
        Tags tags = tagsMapper.selectById(id);
        if (tags != null) {
            result.setSuccess(true);
            result.setMsg("查询成功");
            result.setObj(tags);
            return result;
        }
        result.setSuccess(false);
        result.setMsg("查询失败");
        return result;
    }

    @Override
    public Result deleteById(Integer id) {
        Result result = new Result();
        int delete = tagsMapper.deleteById(id);
        if (delete > 0) {
            result.setSuccess(true);
            result.setMsg("删除成功!");
            return result;
        }
        result.setSuccess(false);
        result.setMsg("删除失败");
        return result;
    }
}
